package com.gjintel.framework.register.dao.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.gjintel.framework.projecttype.model.ProjectType;
import com.gjintel.framework.register.dao.RegisterUserDao;
import com.gjintel.framework.register.model.DepositCategory;
import com.gjintel.framework.register.model.RegisterCategory;
import com.gjintel.framework.register.model.RegisterUser;
import com.gjintel.framework.user.model.User;
import com.gjintel.sdk.base.dao.HibernateGenericDAO;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.util.StringUtil;

@Repository("rUserDao")
public class RegisterUserDaoImpl extends HibernateGenericDAO implements
		RegisterUserDao {

	@Override
	@Transactional
	public void saveRegisterUser(RegisterUser registerUser) {
		try {
			this.save(registerUser);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RegisterUser> findRegisterUserByLoginName(String loginName) {

		return this.find("from RegisterUser u where u.loginName=? and u.removeFlag = 0", loginName);
	}
	

	@SuppressWarnings("unchecked")
	@Override
	public List<RegisterUser> getRegisterList(Page page,RegisterUser registerUser,java.sql.Date startTime,java.sql.Date endTime) {
		List<Object> params = new ArrayList<Object>();
		StringBuffer hql =new StringBuffer("from RegisterUser ru where ru.removeFlag !=1");
		if (!StringUtil.isEmpty(registerUser.getName())) {
			hql.append("and ru.name like?");
			params.add("%" + registerUser.getName() + "%");
		}
		if (!StringUtil.isEmpty(registerUser.getLoginName())) {
			hql.append("and ru.loginName like ?");
			params.add("%" + registerUser.getLoginName() + "%");
		}
		if (registerUser.getFlag() != null && registerUser.getFlag() !=3 && registerUser.getFlag() !=1&&registerUser.getFlag() !=4) {
			hql.append("and ru.flag=" + registerUser.getFlag()+"and ru.removeFlag<>2 ");
		}
		if (registerUser.getFlag() != null && registerUser.getFlag() !=3 && registerUser.getFlag() ==4) {
			hql.append("and ru.removeFlag=2");
		}

		if(registerUser.getFlag() !=null && registerUser.getFlag()==3){
			Date nowtime = new Date();
			hql.append("and ru.forbidEndDate>=? and ru.forbidStartDate<=?");	
			Date now=StringUtil.StringToDate(StringUtil.computeDate(nowtime, "00:00:00", 0));
			params.add(now);
			params.add(now);
		}
		if(registerUser.getFlag() != null && registerUser.getFlag()==1){
			hql.append("and (ru.forbidEndDate is null or ru.forbidEndDate<=?) and (ru.forbidStartDate is null or ru.forbidStartDate>=?) and ru.flag=? and  ru.removeFlag<>2 ");
			Date nowtime = new Date();
			Date now=StringUtil.StringToDate(StringUtil.computeDate(nowtime, "00:00:00", 0));
			params.add(now);
			params.add(now);
			params.add(registerUser.getFlag());
		}
		if(startTime != null){
			hql.append("and ru.createTime >= ? ");
			params.add(startTime);
		}	
	    if(endTime !=null){
	    	hql.append("and ru.createTime <= ? ");
	    	params.add(endTime);
	    }    
	    hql.append("order by ru.createTime desc");	    
	    return  this.findAsPage(hql.toString(), page, params.toArray());
	}

	@Override
	public void auditRegister(String auditId, String registerId, String reason,
			int auth_flag) {
		String hql = "";
		if (auth_flag == 1) {
			hql = "update RegisterUser u set u.id=" + registerId
					+ ", u.auditorId=" + auditId + ",u.flag=" + auth_flag;
		} else if (auth_flag == 2) {
			hql = "update RegisterUser u set u.id=" + registerId
					+ ", u.auditorId=" + auditId + ",u.flag=" + auth_flag
					+ ",u.reason=" + reason;
		}
		this.executeUpdate(hql);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<RegisterUser> getRegisterById(String id) {
		List<RegisterUser> list = new ArrayList<RegisterUser>();
		try {
			list = this.find("from RegisterUser u where u.id=? and u.removeFlag !=1", id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RegisterUser> regcaLogin(String caName) {
		return this.find("from RegisterUser u where u.caCard=? and u.removeFlag != 1", caName);
	}

	@Override
	@Transactional
	public void saveOrUpdateObject(RegisterUser ruser) {
		try {
			this.saveOrUpdate(ruser);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void searchList(Page page, String name, String loginName, int flage) {
		List<String> params = new ArrayList<String>();
		StringBuffer hql = new StringBuffer("from RegisterUser u where 1=1 and u.removeFlag !=1  ");
		if (!StringUtil.isEmpty(name)) {
			hql.append("and u.name like?");
			params.add("%" + name + "%");
		}
		if (!StringUtil.isEmpty(loginName)) {
			hql.append("and u.loginName like ?");
			params.add("%" + loginName + "%");
		}
		if (flage != 4) {
			hql.append("and u.flag=" + flage);
		}
		
		hql.append("order by u.createTime desc");
		try {
			this.findAsPage(hql.toString(), page, params.toArray());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void removeCategories(String registerId) {
		String hql = "DELETE FROM RegisterCategory rc WHERE rc.registerId = '"
				+ registerId + "'";
		this.executeUpdate(hql);

	}

	@Override
	public void removeDepositCategories(String registerId) {
		String hql = "DELETE FROM DepositCategory dc WHERE dc.registerId = '"
				+ registerId + "'";
		this.executeUpdate(hql);

	}

	@Override
	@SuppressWarnings("unchecked")
	public List<DepositCategory> getDepositCategory(String registerId) {
		String hql = "from DepositCategory dc where dc.registerId =?";
		return this.find(hql, registerId);
	}

	@Override
	public ProjectType getProjectTypebyId(String DepositCategoryid) {
		return this.get(ProjectType.class, DepositCategoryid);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<RegisterCategory> getRegisterCategory(String registerId) {
		String hql = "from RegisterCategory rc where rc.registerId =?";
		return this.find(hql, registerId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RegisterUser> getLoginRegisterUserByLoginName(String loginName) {
		
		return this.find("from RegisterUser u where u.loginName=? and u.removeFlag !=1", loginName);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DepositCategory> getDepositCategoryById(String registerId) {
		return this.find("from DepositCategory d where d.registerId=?", registerId);
	}

	@Override
	public RegisterUser getRegisterByLoginname(String loginName) {
		return (RegisterUser)this.findUnique("from RegisterUser u where u.loginName=? and u.removeFlag =0", loginName);		
	}

	@Override
	public User getUserByRegisterId(String registerId) {
		return (User)this.findUnique("from User u where u.registerId=? and u.flag =0", registerId);		
	}

	@Override
	public List<RegisterUser> getSameAccountUser(String account) {
		return this.find("from RegisterUser u where u.account=? and u.removeFlag =0 and u.flag !=2", account);
	}

	@Override
	public List<RegisterUser> getSameAccountUserExceptMyself(String account, String loginName) {
		List<String> params = new ArrayList<>();
		String sql ="from  RegisterUser u  where u.account=? and u.removeFlag =0 and u.flag !=2 and u.loginName !=?";
		params.add(account);
		params.add(loginName);
        return this.find(sql,params.toArray());
	}

}
